package com.sunda.spmswms.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmswms.entity.WhsInventoryInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库库存信息表 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2021-01-28
 */
@Mapper
@Component
public interface WhsInventoryInfoMapper extends BaseMapper<WhsInventoryInfo> {

    @Select("SELECT whs.WERKS AS \"werks\",\n" +
            "  whs.WHS_LOCATION_CODE AS \"whsLocationCode\",\n" +
            "  whs.MATERIAL_NO AS \"materialNo\",\n" +
            "  whs.MATERIAL_ZH_DESC AS \"materialZhDesc\",\n" +
            "  whs.MATERIAL_EN_DESC AS \"materialEnDesc\",\n" +
            "  whs.BASIC_UNIT AS \"basicUnit\",\n" +
            "  whs.QUANTITY AS \"inventoryQty\",\n" +
            "  whs.SAFETY_QUANTITY AS \"safetyQty\"\n" +
            "FROM WHS_INVENTORY_INFO whs WHERE whs.WERKS = #{werks}")
    List<Map<String, Object>> getWhsInventory(String werks);

    @Insert("INSERT INTO WHS_INVENTORY_INFO(WERKS, WHS_LOCATION_CODE, QUANTITY, UPDATE_USER, BOX_NOTE)\n" +
            "VALUES ((SELECT DISTINCT WERKS FROM SAP_DELIVERY_NOTE_DTL DND WHERE DND.DELIVERY_NOTE_SAP = #{deliveryNoteSap}),\n" +
            "        (SELECT DISTINCT WHS_LOCATION_CODE FROM SAP_DELIVERY_NOTE_DTL DND WHERE DND.DELIVERY_NOTE_SAP = #{deliveryNoteSap}),\n" +
            "        1, #{userId}, #{boxNote})")
    int addBoxNoteToWhsInventoryInfo(@Param("deliveryNoteSap") String deliveryNoteSap, @Param("boxNote") String boxNote, @Param("userId") String userId);


    @Select("SELECT QUANTITY FROM WHS_INVENTORY_INFO\n" +
            "WHERE WERKS = #{werks} \n" +
            "  AND WHS_LOCATION_CODE = #{whsLocationCode} AND BOX_NOTE = #{boxNote} AND QUANTITY != 0 \n")
    String getQuantityByBoxNote(@Param("werks") String werks, @Param("whsLocationCode") String whsLocationCode, @Param("boxNote") String boxNote);

    @Select("<script>" +
            "SELECT COUNT(*) FROM WHS_INVENTORY_INFO WHERE BOX_NOTE= #{boxNote} \n" +
            "      <when test='werks!=null'> AND WERKS = #{werks} </when> \n" +
            "      <when test='whsLocationCode!=null'> AND WHS_LOCATION_CODE = #{whsLocationCode} </when> \n" +
            "</script>")
    int validateBoxNote(@Param("boxNote") String boxNote, @Param("werks") String werks, @Param("whsLocationCode") String whsLocationCode);

    /**
     * 根据交货单号查询此单验收任务操作的是
     */
    @Select("SELECT ROWNUM, DN.DELIVERY_NOTE_SAP AS \"deliveryNoteSap\", \n" +
            "       DND.WERKS AS \"werks\", \n" +
            "       DND.WHS_LOCATION_CODE AS \"whsLocationCode\"\n" +
            "FROM SAP_DELIVERY_NOTE DN\n" +
            "    LEFT JOIN SAP_DELIVERY_NOTE_DTL DND ON DN.DELIVERY_NOTE_SAP = DND.DELIVERY_NOTE_SAP\n" +
            "WHERE DN.DELIVERY_NOTE_SAP = #{deliveryNoteSap} AND ROWNUM = 1 ")
    Map<String, Object> getWerksAndWhsLocationCode(@Param("deliveryNoteSap") String deliveryNoteSap);
}
